package com.health.service.impl;

import com.health.dao.ConsultationMapper;
import com.health.dao.ConsultationMessageMapper;
import com.health.dao.DoctorMapper;
import com.health.dao.PatientMapper;
import com.health.pojo.domain.ConsultationDO;
import com.health.pojo.domain.ConsultationMessageDO;
import com.health.pojo.dto.*;
import com.health.service.InteractionService;
import java.lang.Boolean;
import java.lang.Override;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.health.exception.BusinessException;
import com.health.pojo.domain.DoctorDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.Map;
import com.health.pojo.domain.PatientDO;
import com.health.constant.ResultCodeConstant;

/**
 * 医患交互管理的实现
 *
 * @author WUYU_MIN
 * @date 2025-04-29 23:51:18
 */
@Service
public class InteractionServiceImpl implements InteractionService {

    @Autowired
    private PatientMapper patientMapper;

    @Autowired
    private ConsultationMapper consultationMapper;

    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private ConsultationMessageMapper consultationMessageMapper;

    /*
        创建会话
     */
    @Override
    public Boolean createConsultation(ConsultationCreateDTO consultationCreateDTO) {
        // 处理逻辑: 验证患者ID和医生ID是否存在
        Integer patientId = consultationCreateDTO.getPatientId();
        Integer doctorId = consultationCreateDTO.getDoctorId();
        PatientDO patientDO = patientMapper.selectById(patientId);
        DoctorDO doctorDO = doctorMapper.selectById(doctorId);
        if (patientDO == null || doctorDO == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        // 处理逻辑: 创建新的咨询会话记录
        ConsultationDO consultationDO = new ConsultationDO();
        consultationDO.setPatientId(patientId);
        consultationDO.setDoctorId(doctorId);
        consultationDO.setChiefComplaint(consultationCreateDTO.getChiefComplaint());
        consultationMapper.insert(consultationDO);
        return true;
    }

    /*
        获取会话
     */
    @Override
    public ConsultationDO getConsultation(ConsultationQueryDTO consultationQueryDTO) {
        // 处理逻辑: 验证会话ID是否存在
        Integer consultationId = consultationQueryDTO.getConsultationId();
        ConsultationDO consultationDO = consultationMapper.selectById(consultationId);
        if (consultationDO == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        // 处理逻辑: 返回会话信息
        return consultationDO;
    }


    /*
        发送会话
     */
    @Override
    public Boolean sendMessage(ConsultationMessageDTO consultationMessageDTO) {
        // 处理逻辑: 验证会话ID是否存在
        Integer consultationId = consultationMessageDTO.getConsultationId();
        ConsultationDO consultationDO = consultationMapper.selectById(consultationId);
        if (consultationDO == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        // 处理逻辑: 验证发送者ID是否存在
        Integer senderId = consultationMessageDTO.getSenderId();
        if (patientMapper.selectById(senderId) == null && doctorMapper.selectById(senderId) == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        // 处理逻辑: 保存消息到数据库
        ConsultationMessageDO messageDO = new ConsultationMessageDO();
        messageDO.setConsultationId(consultationId);
        messageDO.setSenderId(senderId);
        messageDO.setContentType(consultationMessageDTO.getContentType());
        messageDO.setMessageContent(consultationMessageDTO.getMessageContent());
        consultationMessageMapper.insert(messageDO);
        return true;
    }



    /*
        接收会话
     */
    @Override
    public List<ConsultationMessageDO> receiveMessage(MessageReceiverDTO messageReceiverDTO) {
        // 处理逻辑: 验证会话ID是否存在
        Integer consultationId = messageReceiverDTO.getConsultationId();
        ConsultationDO consultationDO = consultationMapper.selectById(consultationId);
        if (consultationDO == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
//        // 处理逻辑: 验证接收者ID是否存在
//        Integer receiverId = messageReceiverDTO.getReceiverId();
//        if (patientMapper.selectById(receiverId) == null && doctorMapper.selectById(receiverId) == null) {
//            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
//        }
        // 处理逻辑: 查询该会话中发送给接收者的消息
        QueryWrapper<ConsultationMessageDO> queryWrapper = Wrappers.query();
        queryWrapper.eq("consultation_id", consultationId);
        List<ConsultationMessageDO> messages = consultationMessageMapper.selectList(queryWrapper);
        return messages;
    }


    /**
     * 获取对话列表
     *
     * @param comsultationListDTO
     * @return
     */
    @Override
    public List<ConsultationDO> getConsultationList(ComsultationListDTO comsultationListDTO) {
        System.out.println(comsultationListDTO);
        QueryWrapper<ConsultationDO> queryWrapper = Wrappers.query();
        if (comsultationListDTO.getRole().equals("doctor")) {
            queryWrapper.eq("doctor_id", comsultationListDTO.getUserId());
        }
        if (comsultationListDTO.getRole().equals("patient")) {
            queryWrapper.eq("patient_id", comsultationListDTO.getUserId());
        }
        List<ConsultationDO> messages = consultationMapper.selectList(queryWrapper);
        return messages;
    }

    @Override
    public ConsultationDO endConsultation(ConsultationQueryDTO consultationQueryDTO) {
        // 处理逻辑: 验证会话ID是否存在
        Integer consultationId = consultationQueryDTO.getConsultationId();
        ConsultationDO consultationDO = consultationMapper.selectById(consultationId);
        if (consultationDO == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        // 更新会话的结束时间
        consultationMapper.updateEndedAt(consultationId);
        consultationDO.setEndedAt(new Date());
        return consultationDO;
    }
}
